
package com.example.sistemacompras;

import com.example.sistemacompras.db.DatabaseHandler;
import com.example.sistemacompras.entity.Person;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class LoginActivity extends Activity {

    private TextView mostrador;

    private EditText loginEdit;

    private EditText passEdit;

    private Bitmap fotoUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        DatabaseHandler.getInstance(this);
        mostrador = (TextView) findViewById(R.id.painel_text_main);
        fotoUser = null;
        loginEdit = (EditText) findViewById(R.id.login_edit_main);
        passEdit = (EditText) findViewById(R.id.password_edit_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void cadastrar(View view) {
        Intent intent = new Intent(this, SiginUserActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String emailP = loginEdit.getText().toString();
        String passwordP = passEdit.getText().toString();
        Person person = new Person();
        person.setEmail(emailP);
        person.setPassword(passwordP);
        if (fotoUser != null)
            person.setPhoto(fotoUser);
        outState.putSerializable("user", person);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        Person person = (Person) savedInstanceState.get("user");
        loginEdit.setText(person.getEmail());
        fotoUser = person.getPhoto();
        passEdit.setText(person.getPassword());
        ((ImageView) findViewById(R.id.image)).setImageBitmap(fotoUser);
    }

    public void login(View view) {
        String login = loginEdit.getText().toString();
        String pass = passEdit.getText().toString();
        Person person = DatabaseHandler.getPerson(login, pass);
        if (person != null) {
            if ((person.getEmail().equals(login)) && (person.getPassword().equals(pass))) {
                mostrador.setText("Login efetuado. Login:" + person.getEmail());
                if (person.getPhoto() != null) {
                    ((ImageView) findViewById(R.id.image)).setImageBitmap(person.getPhoto());
                    fotoUser = person.getPhoto();
                }
            } else {
                mostrador.setText("Usu&aacute;rio n&atilde;o encontrado");
            }
        } else {
            mostrador.setText("Usu&aacute;rio n&atilde;o encontrado");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        boolean cadastrado = data.getExtras().getBoolean("cadastro");
        if ((requestCode == 1) && (resultCode == RESULT_OK)) {
            TextView text = ((TextView) findViewById(com.example.sistemacompras.R.id.foto_text));
            String nota = cadastrado ? "Cadastro efetuado ... Login = Email"
                    : "N&atilde;o foi possível efetuar cadastro.";

            text.setText(nota);
        }
    }

}
